package com.panghl.controller;

import com.panghl.pojo.User;
import com.panghl.service.UserService;
import jxl.write.WriteException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findPage")
    public List<User>  findPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "10") Integer pageSize){
        return userService.findPage(page,pageSize);
    }

    @GetMapping(value = "/downLoadXlsByJxl",name = "使用jxl导出")
    public void downLoadXlsByJxl(HttpServletResponse response) throws IOException, WriteException {
        //userService.downLoadXlsByJxl(response);
    }


    @PostMapping(value = "/uploadExcel",name = "上传用户数据")
    public void downLoadXlsByJxl(MultipartFile file) throws Exception {
        //userService.uploadExcel(file);
        userService.uploadExcelWithEasyPOI(file);
    }

    @GetMapping(value = "/downLoadXlsxByPoi",name = "使用poi导出")
    public void downLoadXlsxByPoi(HttpServletResponse response) throws IOException, WriteException, InvalidFormatException {
        //userService.downLoadXlsxByPoi(response);
        userService.downLoadXlsxWithTempalte(response);
    }
    @GetMapping(value = "/download",name = "使用poi导出用户详细信息")
    public void downloadUserInfoByTemplate(HttpServletResponse response,Long id) throws Exception {
        //userService.downloadUserInfoByTemplate(id,response);
        //userService.downloadUserInfoByTemplate2(id,response);
        //userService.downloadUserInfoByEasyPOI(id,response);
        userService.downloadUserInfoByPDF(id,response);
    }

    @GetMapping(value = "/downLoadMillion",name = "导出百万数据")
    public void downLoadMillion(HttpServletResponse response) throws Exception {
        userService.downLoadMillion(response);
    }

    @GetMapping(value = "/downLoadCSV",name = "使用csv文件导出百万数据")
    public void downLoadCSV(HttpServletResponse response) throws Exception {
        //userService.downLoadCSV(response); openCsv
        userService.downLoadCSVWithEasyPOI(response);
    }

    @GetMapping(value = "/{id}",name = "根据id查询用户数据")
    public User findById(@PathVariable("id")Long id) throws Exception {
        return userService.findById(id);
    }

    @GetMapping(value = "/downloadContract",name = "下载用户的合同文档")
    public void downloadContract(Long id,HttpServletResponse response) throws Exception {
         //userService.downloadContract(id,response);
         userService.downloadContractWithEasyPOI(id,response);
    }

    @GetMapping(value = "/downLoadWithEasyPOI",name = "使用EasyPoi方式导出excel")
    public void downLoadWithEasyPOI(HttpServletResponse response) throws Exception {
        userService.downloadWithEasyPoi(response);
    }

    @GetMapping(value = "/downLoadPDF",name = "jasper导出用户数据到PDF")
    public void downLoadPDF(HttpServletResponse response) throws Exception {
        //userService.downLoadPDF(response);
        userService.downLoadPDF2(response);
    }

    @GetMapping(value = "/jfreeChart",name = "jfreeChart")
    public void jfreeChart(HttpServletResponse response) throws Exception {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset(); //饼状数据集
        dataset.setValue(180,"技术部","2011年");
        dataset.setValue(99,"技术部","2012年");
        dataset.setValue(6,"销售部","2011年");
        dataset.setValue(8,"销售部","2012年");
        dataset.setValue(10,"人事部","2011年");
        dataset.setValue(23,"人事部","2012年");

        StandardChartTheme chartTheme = new StandardChartTheme("CN");
        //设置大标题的字体
        chartTheme.setExtraLargeFont(new Font("楷体",Font.BOLD,20));
        //设置图例的字体
        chartTheme.setRegularFont(new Font("楷体",Font.BOLD,15));
        //设置图片的内容  xy轴
        chartTheme.setLargeFont(new Font("楷体",Font.BOLD,15));
        ChartFactory.setChartTheme(chartTheme);

        //String title, String categoryAxisLabel, String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls
        // 大标题      X轴的说明           Y轴的说明           数据集                                                         是否显示图例      是否显示提示(CS架构)    是否跳转(CS架构)
        JFreeChart jfc= ChartFactory.createBarChart("这是折线图标题","公司","入职人数", dataset);
        ChartUtils.writeChartAsJPEG(response.getOutputStream(),jfc,400,300);
    }


}
